package com.rocky.project.action.model;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.opensymphony.webwork.ServletActionContext;
import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.family.Archive;
import com.rocky.project.model.family.Attachment;
import com.rocky.project.util.FileUtil;
import com.rocky.project.util.WebUtil;


public class AttachmentAction extends BaseAction {
	private static final long serialVersionUID = -3510474656970657392L;
	//文件上传start
	private File Filedata;
	private String FiledataFileName;
	private String FiledataContentType;
	private Attachment attachment;
	private String relativePath;
	private void uploadFile(Archive archive) throws IOException {		
		String dateStr=DateFormatUtils.format(Calendar.getInstance().getTime(), "yyyyMMddHHmmssms-");
		String realBaseDir=ServletActionContext.getServletContext().getRealPath("/upload/"+archive.getFamily().getHouseholdername()+"/"+ dateStr.substring(0, 6));
		File baseFile=new File(realBaseDir);
		if(!baseFile.exists()){
			baseFile.mkdir();
		}		
		String lastStr = String.valueOf(Math.random());
		relativePath =dateStr + lastStr.substring(lastStr.length()-1);
		relativePath = FileUtil.getDocumentPath(realBaseDir, FiledataFileName, relativePath);		
		FileUtil.copyFile(Filedata, new File(relativePath));	
	}		
	public String uploadImage() throws Exception {		
		Archive archive=(Archive) baseService.get(Archive.class, Long.valueOf(id));	
		String type = WebUtil.getParaValue("type");
		uploadFile(archive);
		attachment=new Attachment();
		attachment.setFamily(archive.getFamily());
		attachment.setArchive(archive);
		attachment.setSize(Filedata.length());
		attachment.setDisplayName(FiledataFileName);
		attachment.setRelativePath(relativePath.substring(relativePath.indexOf("upload")));		
		attachment.setName(FiledataFileName);	
		attachment.setType(type);
		WebUtil.init4create(attachment);
		baseService.create(attachment);			
		ServletActionContext.getResponse().setContentType("text/plain charset=\"utf-8\"");
		ServletActionContext.getResponse().getWriter().write("ok");
		return NONE;		
	}
	@SuppressWarnings("unchecked")

	public String read() throws Exception {		
		
		List<Object> paramsvalues = new LinkedList<Object>();		
		String queryHql = "SELECT m FROM Attachment AS m WHERE m.deleted = false   ";
		
		if(id!=null && !id.equals("") ){
			queryHql +="    and m.family.id = ?  ";			
			paramsvalues.add(Long.valueOf(id));		
		}		
		String type = WebUtil.getParaValue("type");
		if(type!=null && !type.equals("") ){
			queryHql +="    and m.type = ?  ";			
			paramsvalues.add(type);			
		}
		queryHql += "  order by m.id desc   ";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),getStart(), getPageSize());
		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());		
		this.ajaxResultMap.put("rowCount", rowCount);
		this.ajaxResultMap.put("data", pageRecords);
		if(StringUtils.isNotBlank(this.suc)){			
			return this.suc;
		}
		return ajaxResult;
	}
	

	
	@Override
	public String deleteProcess() throws Exception {
		String[] ids = WebUtil.getParaValue("ids").split(",");
		for(String id :ids){
			Attachment attchment = (Attachment) baseService.get(Attachment.class, Long.valueOf(id));		
			try {
				File file = new File(ServletActionContext.getServletContext().getRealPath(attchment.getRelativePath()));
				if (file.exists()) {
					file.delete();
					log.info(" ###file.delete!");
				}else{
					log.info(" ###file.not.exists!");
				}
				
			} catch (Exception e) {
				log.error(" ### 文件删除异常 :" + e.toString());
			}
			baseService.deleteTrue(attchment);	
			
		}		


		return ajaxResult;
	}
	public Attachment getAttachment() {
		return attachment;
	}
	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}
	public File getFiledata() {
		return Filedata;
	}
	public void setFiledata(File filedata) {
		Filedata = filedata;
	}
	public String getFiledataContentType() {
		return FiledataContentType;
	}
	public void setFiledataContentType(String filedataContentType) {
		FiledataContentType = filedataContentType;
	}
	public String getFiledataFileName() {
		return FiledataFileName;
	}
	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}

}
